package com.atguigu;

import io.minio.*;
import io.minio.errors.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static void main(String[] args) {
        try {
            //创建Minio的客户端对象
            MinioClient minioClient = MinioClient.builder()
                    .endpoint("http://192.168.100.99:9000")
                    .credentials("minioadmin", "minioadmin").build();

            //桶的名字
            String bucketName = "lease";

            //判断桶是否存在
            boolean result = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());

            //创建桶
            if (!result){
                //桶不存在,则创建新的桶
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
                String policyConfig = """
                        {
                          "Statement" : [ {
                            "Action" : "s3:GetObject",
                            "Effect" : "Allow",
                            "Principal" : "*",
                            "Resource" : "arn:aws:s3:::lease/*"
                          } ],
                          "Version" : "2012-10-17"
                        }
                        """;
                //设置桶的访问权限
                minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName).config(policyConfig).build());
            }

            //上传文件
            minioClient.uploadObject(UploadObjectArgs.builder().bucket(bucketName).filename("d://pic/1.jpg").object("1.jpg").build());
            System.out.println("上传成功");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}